# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=shared-mime-info
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0
pkgrel=5
pkgdesc="Freedesktop.org Shared MIME Info (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=(GPL2)
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-glib2")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-itstool"
             "${MINGW_PACKAGE_PREFIX}-gettext")
install=${_realname}-${MSYSTEM}.install
url="https://freedesktop.org/Software/shared-mime-info"
source=(https://gitlab.freedesktop.org/xdg/shared-mime-info/-/archive/${pkgver}/shared-mime-info-${pkgver}.tar.bz2
        program.manifest
        001-fix-fdo-generate.patch)
sha256sums=('d8acf1ed1c79d9fadcd2cec27a9a9dbc660ac37a0485a6e2f73c2bd7db195e4a'
            'f93191ad3e217ef9939cf16885ffe50ce920a0b639e183e46415f10df4b4db72'
            'e49b3232579dad5da4bbdc68980f30f7f9f36f75a32c6b6fe1357a8b788e1935')
options=(!makeflags)

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/001-fix-fdo-generate.patch
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Dupdate-mimedb=false \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/ninja
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ninja install

  [[ ${CARCH} == i686 ]] && {
    _arch=x86
  } || {
    _arch=amd64
  }
  install -Dm644 ${srcdir}/program.manifest ${pkgdir}${MINGW_PREFIX}/bin/update-mime-database.exe.manifest
  sed -e "s|%PROGNAME%|GNU.update-mime-database|g" \
      -e "s|%ARCH%|${_arch}|g" \
      -i ${pkgdir}${MINGW_PREFIX}/bin/update-mime-database.exe.manifest
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
